#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2020/6/10 0010 22:12
# @Author  : wzy
# @Site    : 
# @File    : configEmail.py
# @Software: PyCharm

import os
import smtplib
from email.utils import formataddr
from testFile.readconfig import readConfig
from email.mime.multipart import MIMEMultipart
from email.mime.application import MIMEApplication
from testFile.getpathInfo import get_path
sub = readConfig.get_email('subject')
address = readConfig.get_email('addressee')
cc = readConfig.get_email('cc')
acceptCode = readConfig.get_email('acceptCode')
mail_path = os.path.join(get_path(), 'result', 'report.html')
def send():
    '''
    带附件
    :return:
    '''
    ret = True
    # msg = MIMEText('shuru you jian neirong','plain','utf-8')
    # msg['From'] = formataddr(["FROM",address])
    # msg['To'] = formataddr(["To",address])
    # msg['Subject'] = sub
    #---- 附件用  MIMEApplication
    msg = MIMEMultipart()
    msg['From'] = formataddr(["FROM",address])
    msg['To'] = formataddr(["To",address])
    msg['Subject'] = sub
    # xlsx类型附件
    part = MIMEApplication(open(mail_path, 'rb').read())
    part.add_header('Content-Disposition', 'attachment', filename="report.HTML")
    msg.attach(part)
    try:
        server = smtplib.SMTP_SSL("smtp.qq.com", 465)  # 发件人邮箱中的SMTP服务器，端口是25
        server.login(address, acceptCode)  # 括号中对应的是发件人邮箱账号、邮箱密码
        server.sendmail(address,[address,],msg.as_string())  # 括号中对应的是发件人邮箱账号、收件人邮箱账号、发送邮件
        server.quit()  # 关闭连接
        print("已发送")
        return ret
    except smtplib.SMTPException:
        print("SMTP is null")
    return ret
if __name__ == '__main__':
    #print(get_path())
   # print(sub + app + cc +address + mail_path)
    send()
    #print(mail_path)
    pass


